class DataFile < ActiveRecord::Base
  def self.save(upload, extension, id)
    
    directory = "public/images/pictures"
    name =  id.to_s + "." + extension
    nameSmall = id.to_s + "-small."+extension
    
    
    # create the file path
    path = File.join(directory, name)
    pathSmall = File.join(directory, nameSmall)
    
    # write the file
    File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
    
    # write the small file
    img =  Magick::Image.read(path).first
    thumb = img.resize_to_fit(150,150)
    thumb.write(pathSmall)
  end
end
